var net = require('net');

var HOST = '127.0.0.1';
var PORT = 7000;
var numOfClient = 10;
var waitTime = 1000;
Message = function(_type,_content){
	this.content = _content;
	this.type = _type ;
}
SensorOutput = function(_devName,_devNo,_content,_value,_date){
	this.DevName = _devName;
	this.DevNo = _devNo;
	this.MonitorContent = _content;
	this.MonitorValue = _value;
	this.MonitorDate = _date;
};
Client = function(_No,_socket){
	this.DevNo = _No;
	this.DevName = "sensor"+_No;
	this.socket = _socket;
}
var clients = [] ;
for(var i=0;i<numOfClient;i++){
//*	
	var socket = new net.Socket();
	var client = new Client(i,socket);
	client.socket.connect(PORT, HOST, function() {
		console.log('CONNECTED TO: ' + HOST + ':' + PORT);
	});
	client.socket.on('data',function(data){
		console.log('return from server:'+data.toString());
	});
	client.socket.on('error',function(err){
		console.log("error");
	});
	clients.push(client);//*/
}
var num = 0;
TickThread = function(){
	//*
	//clients.forEach(function(subClient){
		//console.log("Test"+num);
		var subClient = clients[Math.floor(Math.random()*numOfClient)];
		var date = new Date();
		//var sensorOutput = new SensorOutput(subClient.DevName,subClient.DevNo,"RealTimeOutput",Math.floor(Math.random()*100),date.toLocaleTimeString());
    var sensorOutput = new SensorOutput(subClient.DevName,subClient.DevNo,"RealTimeOutput",Math.floor(Math.random()*100),date.toLocaleString());
		var msg = new Message(1,JSON.stringify(sensorOutput));
		var jsonStr = JSON.stringify(msg);
		subClient.socket.write(jsonStr);
		console.log(jsonStr);
	//});//*/
}
setInterval(TickThread,waitTime);
